limsOutsourcers.editPage = {

    getGridParam: function () {
        var _this = this;
        return {
            type: 'grid',
            id: 'lims-outsourcers-edit-list-grid',
            url: limsOutsourcers.baseUrl + '/queries/raw',
            service: 'limsOutsourcersServiceImpl',
            dbTable: 'T_LIMS_OUTSOURCERS',
            columns: limsOutsourcers.getEditListColumns(),
            onCellClick: function (field, row) {
                field === 'outsourcerCode' && _this.loadDetailPage(row.id, 'edit')
            },
            page: true,
            deleteFormatter: function (row) {
                return Gikam.propI18N('T_LIMS_OUTSOURCERS.OUTSOURCERCODE') + ': ' + row.outsourcerCode
            },
            requestData: {
                activedFlag_EQ: '1'
            },
            toolbar: [{
                type: 'button',
                text: 'GIKAM.BUTTON.INSERT',
                icon: 'add',
                onClick: function () {
                    Gikam.preInsert({
                        modalTitle: 'BASEMODULE.BUSINESS.OUTSOURCERSS.MODAL.ADD',
                        fields: limsOutsourcers.getBaseInfoFormFields(),
                        disableNotEmptyValidate: true,
                        url: limsOutsourcers.baseUrl
                    }).done(function (id) {
                        Gikam.getComp('lims-outsourcers-edit-list-grid').refresh();
                        _this.loadDetailPage(id, 'edit')
                    });
                }
            }, {
                type: 'button',
                text: 'GIKAM.BUTTON.DELETE',
                icon: 'remove-row',
                onClick: function () {
                    Gikam.getComp('lims-outsourcers-edit-list-grid').deleteRows(limsOutsourcers.baseUrl);
                }
            }, {
                type: 'button',
                text: 'GIKAM.BUTTON.DEACTIVE',
                icon: 'disable',
                onClick: function () {
                    _this.setActivedFlag('lims-outsourcers-edit-list-grid', '0')
                }
            }]
        }
    },

    getDisableGridParam: function () {
        var _this = this;
        return {
            type: 'grid',
            id: 'lims-outsourcers-edit-list-grid-disable',
            url: limsOutsourcers.baseUrl + '/queries/raw',
            service: 'limsOutsourcersServiceImpl',
            dbTable: 'T_LIMS_OUTSOURCERS',
            columns: limsOutsourcers.getEditListColumns(),
            onCellClick: function (field, row) {
                field === 'outsourcerCode' && _this.loadDetailPage(row.id, 'readonly')
            },
            page: true,
            deleteFormatter: function (row) {
                return Gikam.propI18N('T_LIMS_OUTSOURCERS.OUTSOURCERCODE') + ': ' + row.outsourcerCode
            },
            requestData: {
                activedFlag_EQ: '0'
            },
            toolbar: [{
                type: 'button',
                text: 'BASEMODULE.BUSINESS.OUTSOURCERSS.BUTTON.ACTIVE',
                icon: 'activate',
                onClick: function () {
                    _this.setActivedFlag('lims-outsourcers-edit-list-grid-disable', '1')
                }
            }]
        }
    },

    setActivedFlag: function (gridId, status) {
        let url = limsOutsourcers.baseUrl + '/set/activedflag'
        var grid = Gikam.getComp(gridId)
        var selections = grid.getSelections()
        if (Gikam.isEmpty(selections)) {
            Gikam.alert('GIKAM.TIP.CHOOSE_ONE_ITEM')
            return
        }
        Gikam.confirm('CORE.MODULE.ITEM.MESSAGES.TIP.CONFIRM', 'BASEMODULE.BUSINESS.OUTSOURCERSS.TIP.SET_ACTIVEDFLAG', function () {
            Gikam.postText(url, Gikam.getJsonWrapper({status: status}, [null, selections])).done(function (data) {
                grid.refresh()
            })
        })
    },

    getMainTabs: function () {
        var _this = this
        var first = false
        return {
            type: 'tab',
            id: 'main-tab',
            panels: [{
                title: 'GIKAM.ACTIVE.STATUS.YES',
                items: [_this.getGridParam()]
            }, {
                title: 'GIKAM.ACTIVE.STATUS.NO',
                items: [_this.getDisableGridParam()]
            }],
            onActived: function (data) {
                switch (data.index) {
                    case 0:
                        if (first) {  // 首次加载页面时，不操作
                            Gikam.getAsyncComp('lims-outsourcers-edit-list-grid').done(function (grid) {
                                grid.refresh()
                            })
                        }
                        break
                    case 1:
                        Gikam.getAsyncComp('lims-outsourcers-edit-list-grid-disable').done(function (grid) {
                            grid.refresh({
                                url: limsOutsourcers.baseUrl + '/queries/raw',
                            })
                        })
                        break
                }
                first = true
            }
        }
    },

    loadDetailPage: function (id, step) {
        workspace.window.load(limsOutsourcers.baseUrl + '/page/detail' + Gikam.param({
            id: id,
            step: step
        }));
    },

    create: function () {
        var _this = this;
        Gikam.create('layout', {
            id: 'lims-outsourcers-edit-list-layout',
            renderTo: workspace.window.$dom,
            center: {
                items: [this.getMainTabs()]
            }
        });
    },

    init: function () {
        this.create();
    }
};
